import { _decorator, Animation, AnimationClip, Component, Node, SpriteFrame } from 'cc';
import State from './State';
import { FSM_PARAM_TYPE_ENUM } from '../Enums';
import { SubStateMachine } from './SubStateMachine';
const { ccclass, property } = _decorator;

type ParamValueType = boolean | number

export interface IParamValue {
    type: FSM_PARAM_TYPE_ENUM
    value: ParamValueType
}

export const getInitParamsTrigger = () => {
    return {
        type: FSM_PARAM_TYPE_ENUM.TRIGGER,
        value: false
    }
}

export const getInitParamsNumber = () => {
    return {
        type: FSM_PARAM_TYPE_ENUM.NUMBER,
        value: 0
    }
}


@ccclass('StateMachine')
export abstract class StateMachine extends Component {

    private _currentState: State | SubStateMachine = null

    params: Map<string, IParamValue> = new Map()

    stateMachine: Map<string, State | SubStateMachine> = new Map()

    animationComponent: Animation

    waitingList: Array<Promise<SpriteFrame[]>> = []

    getParams(paramName: string) {
        if (this.params.has(paramName)) {
            return this.params.get(paramName).value
        }
    }

    setParams(paramName: string, value: ParamValueType) {
        console.info(this.params, paramName)
        if (this.params.has(paramName)) {
            this.params.get(paramName).value = value
            this.run()
            this.reset()
        }
    }

    get currentState() {
        return this._currentState
    }

    set currentState(newState) {
        console.info('currentState', newState)
        this._currentState = newState
        this._currentState.run()
    }

    reset() {
        for (const [_, value] of this.params) {
            if (value.type === FSM_PARAM_TYPE_ENUM.TRIGGER) {
                value.value = false
            }
        }
    }

    abstract init(): void

    abstract run(): void

}


